/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import modelo.Cliente;
import modelo.DAO;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

/**
 *
 * @author odeen
 */
public class EditarClienteController extends SimpleFormController {

    public EditarClienteController() {
        setCommandClass(Cliente.class);
        setCommandName("cliente");
    }

    /**
     * Será invocado cuando se procese el formulario
     * @param command
     * @param errors
     * @return
     * @throws Exception
     */
    @Override
    protected ModelAndView onSubmit(Object command, BindException errors) throws Exception {
        System.out.println("onSubmit");
        Cliente c = (Cliente) command;
        System.out.println(c);
        dao.actualizar(c);
        return super.onSubmit(command, errors);
    }
    /**
     * Permite inicializar datos adicionales, p.e listas de provincias
     * @param request
     * @return
     * @throws Exception
     */
    @Override
    protected Map referenceData(HttpServletRequest request) throws Exception {
        return super.referenceData(request);
    }
    /**
     *
     * @param request
     * @return
     * @throws Exception
     */
    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        if("POST".equals(request.getParameter("POST"))){
            return super.formBackingObject(request);            
        }else{
            Cliente c = dao.buscarClientePorId(Integer.parseInt(request.getParameter("id")));
            return c;
        }
    }

    protected DAO dao;

    public DAO getDao() {
        return dao;
    }

    public void setDao(DAO dao) {
        this.dao = dao;
    }


}
